package com.test.speach2text;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;


public class Main extends Activity {
	// Declare constant to be used as code for speach2text API call
	static final int SPEACH_2_TEXT_CODE = 4534;
	
	// Declare layout to display results
	LinearLayout display_view;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        // Initialize layout to display results
        display_view = (LinearLayout) findViewById(R.id.displayLayout);
        
        // Initialize button to start speach2text call
        Button speak = (Button) findViewById(R.id.speak);
    
        // Define onClickListener for speak
        speak.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Clear any results that were previously displayed in display_view
				display_view.removeAllViews();
				
				// Start intent to be used to start speach to text activity
				Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
				
				// Display an hint to the user about what he should say.
				intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something bitch!");

				// Given an hint to the recognizer about what the user is going to say
				intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
						RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

				// Specify how many results you want to receive. The results will be sorted
					// where the first result is the one with higher confidence.
				intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);

				// Start speach to text activity using SPEACH_2_TEXT_CODE
				startActivityForResult(intent, SPEACH_2_TEXT_CODE);	
			}
		});
    }
    
    /**
	 * Handle the results from the recognition activity.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// Check if this was a speach2text request and result is ok
		if (requestCode == SPEACH_2_TEXT_CODE && resultCode == RESULT_OK) {
			// Fill the list view with the strings the recognizer thought it could have heard
			ArrayList<String> matches = data.getStringArrayListExtra(
					RecognizerIntent.EXTRA_RESULTS);

			// Iterate through result set
			for(int i = 0; i < matches.size(); i++){
				// Create new TextView
				TextView temp_text = new TextView(getApplicationContext());
				
				// Make the text of this TextView to be this result
				temp_text.setText(matches.get(i));
				
				// Define layout parameters to be used for this TextView
				LinearLayout.LayoutParams temp_params = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
				
				// Set the layout gravity of this TextView to be center
				temp_params.gravity = android.view.Gravity.CENTER;
				
				// Set the left and right margins of this text to be 10 and the top and 
					// bottom margins to be 5
				temp_params.setMargins(10, 5, 10, 5);
				
				// Set the layout parameters for this TextView
				temp_text.setLayoutParams(temp_params);
				
				// Add this TextView to the result display layout
				display_view.addView(temp_text);
			}
		}
			
        // Call super function
		super.onActivityResult(requestCode, resultCode, data);
	}
}